package com.wzdigit.wms.basic.service.code;

import com.wzdigit.framework.Shift;
import com.wzdigit.mes.sys.api.UserApiService;
import com.wzdigit.mes.sys.dto.UserDto;
import com.wzdigit.wms.basic.client.response.basic.WarehouseApiResponse;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库权限--服务类
 * 
 * @author Administrator
 *
 */
@Service
public class WhsPermService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    
    @Reference(version = "${dubbo.consumers.sys.user.api.version:1.0.0}", check = false, timeout = 300000)
    private UserApiService userApiService;

    /*---------------------------------------------1--------------------------------------------*/
    /**
	 * 获取当前用户权限的仓库ID
	 * 
	 * @param servletRequest
	 * @return
	 */
    public List<Integer> getWareHouseIds(HttpServletRequest servletRequest) {
    	String userName = HttpSessionUtil.getLoginName(servletRequest);
		return getWareHouseIds(userName);
 	}
    
    /**
     * 获取当前用户权限的仓库ID
     * 
     * @param whsType
     * @param servletRequest
     * @return
     */
    public List<Integer> getWareHouseIds(Integer whsType, HttpServletRequest servletRequest) {
    	String userName = HttpSessionUtil.getLoginName(servletRequest);
		return getWareHouseIds(userName, whsType);
 	}
    
    
    
    /*---------------------------------------------2--------------------------------------------*/
    /**
	 * 获取当前用户权限的仓库
	 * 
	 * @param servletRequest
	 * @return
	 */
	public List<WarehouseApiResponse> getWarehousePerm(HttpServletRequest servletRequest) {
		String userName = HttpSessionUtil.getLoginName(servletRequest);
		return getWarehousePerm(userName);
	}
	
	/**
	 * 获取当前用户权限的仓库
	 * 
	 * @param whsType
	 * @param servletRequest
	 * @return
	 */
	public List<WarehouseApiResponse> getWarehousePerm(Integer whsType, HttpServletRequest servletRequest) {
		String userName = HttpSessionUtil.getLoginName(servletRequest);
		return getWarehousePerm(userName, whsType);
	}
    
	
	
	/*---------------------------------------------3--------------------------------------------*/
	/**
	 * 获取指定用户权限的仓库ID
	 * 
	 * @param userName
	 * @return
	 */
    public List<Integer> getWareHouseIds(String userName) {
 		List<WarehouseApiResponse> whList = getWarehousePerm(userName);
 		List<Integer> wareHouseIds = whList.stream().map(WarehouseApiResponse::getWhsId).distinct().collect(Collectors.toList());
 		return wareHouseIds;
 	}
    
    /**
     * 获取指定用户权限的仓库ID
     * 
     * @param userName
     * @param whsType
     * @return
     */
    public List<Integer> getWareHouseIds(String userName, Integer whsType) {
 		List<WarehouseApiResponse> whList = getWarehousePerm(userName, whsType);
 		List<Integer> wareHouseIds = whList.stream().map(WarehouseApiResponse::getWhsId).distinct().collect(Collectors.toList());
 		return wareHouseIds;
 	}
    
    
    
    /*---------------------------------------------4--------------------------------------------*/
    /**
	 * 获取指定用户权限的仓库
	 * 
	 * @param userName
	 * @return
	 */
	public List<WarehouseApiResponse> getWarehousePerm(String userName) {
		return getWarehousePerm(userName, null);
	}
	
	/**
	 * 获取指定用户权限的仓库
	 * 
	 * @param userName
	 * @param whsType
	 * @return
	 */
	public List<WarehouseApiResponse> getWarehousePerm(String userName, Integer whsType) {
		UserDto userDto = userApiService.getUserByUsername(userName);
		if (userDto == null || userDto.getUserId() == null) {
			Shift.fatal("当前登录用户没有信息");
		}
		int userId = userDto.getUserId().intValue();
		List<WarehouseApiResponse> wareHouseList = warehouseMapper.selectAllByUserIdAndWhsType(userId, whsType);
		if (wareHouseList.size() == 0) {
 			Shift.fatal("当前用户未维护权限仓库！");
 		}
		return wareHouseList;
	}

    /**获取用户有库位的权限ID
        * @param userName
        * @return java.util.List<java.lang.String>
        * @author shuaic
        * @date 2021/10/25 22:12
    */
	public List<Integer> getZonePerm(String userName) {
		List<Integer> zones=  warehouseMapper.selectZoneIdsByUserName(userName);
		if (zones.size() == 0) {
			Shift.fatal("当前用户未维护库位权限！");
		}
		return zones;
	}

}
